package severlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import dao.UserMapper;
import model.User;
import util.SqlSessionUtil;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
/**
* @author 听风寂  ，林舒馨
* @date 2020/1/8
* @ClassName RegisterServlet
*/
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//获取JSON的数据
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String telephone = request.getParameter("telephone");
		String sex = request.getParameter("sex");
		
		//写入个人信息
		User user = new User(username, password, name, email, telephone, sex);
		SqlSession s=SqlSessionUtil.getSqlSession();
		UserMapper up =s.getMapper(UserMapper.class);
		int flag=up.insertUser(user);
		System.out.println(user.toString());
		JSONObject object=new JSONObject();
		PrintWriter out = response.getWriter();
		object.put("msg", "");
		object.put("count", 3);	
		if(flag==0) {
			object.put("code","0");
		}
		else{
			System.out.println(flag);
			s.commit();
			object.put("code","1");
			}
		String jsonStr = JSON.toJSONString(object);
		out.print(jsonStr);
		out.close();	
				
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
